package com.yjhb.android.sbz.activity;

import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;

import androidx.viewpager.widget.ViewPager;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.fragment.taskmanage.DisplayFragment;
import com.yjhb.android.sbz.fragment.taskmanage.FailFragment;
import com.yjhb.android.sbz.fragment.taskmanage.OffFragment;
import com.yjhb.android.sbz.fragment.taskmanage.SuspendFragment;
import com.yjhb.android.sbz.fragment.taskmanage.ViolationFragment;
import com.yjhb.android.sbz.fragment.taskmanage.WaitFragment;
import com.yjhb.android.sbz.tool.MsgReadTool;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.adapter.FragmentAdapter;
import com.jojo.android.zxlib.base.BaseFragment;
import com.jojo.android.zxlib.base.mvp.BasePresenter;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.jojo.android.zxlib.tool.ZxTool;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.tencent.qcloud.tim.uikit.UnReadManager;
import com.tencent.qcloud.tim.uikit.config.SysUserConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/11/20 17:20
 */
public class ManageTaskActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnTouchListener {
    private final String TAG = "ManageTaskActivity";

    @BindView(R.id.tabLayout)
    QMUITabSegment tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.publish_iv)
    View publish_iv;

    List<BaseFragment> fragments = new ArrayList<>();
    WaitFragment waitFragment;
    DisplayFragment displayFragment;
    SuspendFragment suspendFragment;
    FailFragment failFragment;
    OffFragment offFragment;
    ViolationFragment violationFragment;
    FragmentAdapter fragmentAdapter;

    private int currIndex;
    private MsgReadTool readTool;


    @Override
    protected BasePresenter initPresenter() {
        return null;
    }


    @Override
    protected boolean isImmerse() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_manage_task;
    }

    @Override
    protected void initUI() {
        currIndex = getIntent().getIntExtra("index", 1);
        initFragments();
        readTool = new MsgReadTool();
        readTool.loadSystemNotice(SysUserConfig.CHECK_NOTICE.getId());
        UnReadManager.getInstance().setUncheck(0);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onDestroy() {
        if (readTool != null) {
            readTool.destroyChat();
        }
        super.onDestroy();
    }

    @Override
    protected void regEvent() {
        publish_iv.setOnTouchListener(this);
        viewpager.addOnPageChangeListener(this);
    }

    @OnClick(R.id.back)
    public void back() {
        finish();
    }

    @OnClick(R.id.title_right_tv)
    public void title_right_tv() {
        Tool.showPopupRules(this, findViewById(R.id.title_right_tv));
    }


    private void initFragments() {
        fragments.clear();
        fragments.add(waitFragment = new WaitFragment());
        fragments.add(displayFragment = new DisplayFragment());
        fragments.add(suspendFragment = new SuspendFragment());
        fragments.add(failFragment = new FailFragment());
        fragments.add(offFragment = new OffFragment());
        fragments.add(violationFragment = new ViolationFragment());
        viewpager.setAdapter(fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments));

        tabLayout.setDefaultNormalColor(getResources().getColor(R.color.gray_66));
        tabLayout.setDefaultSelectedColor(getResources().getColor(R.color.colorPrimaryDark));
        tabLayout.setTabTextSize(ZxTool.dip2px(14));
        tabLayout.setupWithViewPager(viewpager);

        viewpager.setCurrentItem(currIndex);
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        currIndex = i;
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @OnClick(R.id.publish_iv)
    public void publish_iv() {
        startActivity(new Intent(this, PublishTypeActivity.class));
    }

    boolean isMove;
    float srcX, srcY;

    /**
     * 发布圆球拖动
     *
     * @param v
     * @param event
     * @return
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            srcX = event.getRawX();
            srcY = event.getRawY();
        }
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            ZxLogTool.i("移动中 X:" + Math.abs(event.getRawX() - srcX) + "  Y:" + Math.abs(event.getRawY() - srcY));
            if (Math.abs(event.getRawX() - srcX) > 10 || Math.abs(event.getRawY() - srcY) > 10) {
                isMove = true;
                v.setX(v.getX() + event.getX() - v.getWidth() / 2);
                v.setY(v.getY() + event.getY() - v.getHeight() / 2);
            }
        }
        if (event.getAction() == MotionEvent.ACTION_CANCEL || event.getAction() == MotionEvent.ACTION_UP) {
            ZxLogTool.i("抬起手指");
            if (isMove) {
                ZxLogTool.i("移动后抬起手指");
                isMove = false;
                return true;
            }
        }
        return false;
    }
}
